package com.ruoyi.common.utils.kuaidi100.result;

import com.ruoyi.common.utils.kuaidi100.RType;
import lombok.Data;

import java.io.Serializable;

/**
 * 返回的数据格式
 *
 * @author 王松
 * @WX-QQ 1720696548
 * @date 2019/11/14 14:55
 */

@Data
public class R<T> implements Serializable {

    private static final long serialVersionUID = -5666504070515657048L;

    private Integer code;

    private String msg;

    //@JsonInclude(value = JsonInclude.Include.NON_EMPTY) //空不返回
    private T data;

    private String errorMsg;

    /**
     *   枚举方式
     *
     * @param RType
     * @param data
     * @return
     * @author ws
     * @mail 1720696548@qq.com
     * @date 2020/4/19 0019 14:55
     */
    public R(RType RType, T data, String errorMsg) {
        this.code = RType.getValue();
        this.msg = RType.getMsg();
        this.data = data;
        this.errorMsg = errorMsg;
    }

    /**
     * code，msg方式
     *
     * @param code
     * @param msg
     * @param data
     * @return
     * @author ws
     * @mail 1720696548@qq.com
     * @date 2020/4/19 0019 14:55
     */
    public R(Integer code, String msg, T data, String errorMsg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
        this.errorMsg = errorMsg;
    }

    // @formatter:off
    // 通用返回成功
    public static R<Void> success() {return success(null); }
    public static <T> R<T> success(T data) { return new R(RType.SYS_SUCCESS, data, null);}

    // 查询成功
    public static R<Void> successFind() { return successFind(null);}
    public static <T> R<T> successFind(T data) { return new R(RType.SYS_SUCCESS_FIND, data, null); }

    // 添加成功
    public static R<Void> successInsert() {return successInsert(null); }
    public static <T> R<T> successInsert(T data) {
        return new R(RType.SYS_SUCCESS_INSERT, data, null);
    }

    // 编辑成功
    public static R<Void> successUpdate() {
        return successUpdate(null);
    }
    public static <T> R<T> successUpdate(T data) { return new R(RType.SYS_SUCCESS_UPDATE, data, null); }

    // 删除成功
    public static R<Void> successDelete() {  return successDelete(null); }
    public static <T> R<T> successDelete(T data) { return new R(RType.SYS_SUCCESS_DELETE, data, null); }

    // 返回失败（传入自定义错误code+msg）+ （传入自定义枚举）
    public static <T> R<T> error(RType RType) {  return error(RType.getValue(),RType.getMsg(),null,null);}
    public static <T> R<T> error(RType RType, String errorMsg) { return error(RType.getValue(),RType.getMsg(),null, errorMsg);}
    public static <T> R<T> error(RType RType, String data, String errorMsg) {return error(RType.getValue(),RType.getMsg(),data,errorMsg);}
    public static <T> R<T> error(Integer code, String msg) {return new R(code, msg,null,null);}
    public static <T> R<T> error(Integer code, String msg, String errorMsg) {return new R(code, msg,null,errorMsg);}
    public static <T> R<T> error(Integer code, String msg,String data, String errorMsg) {return new R(code, msg,data,errorMsg);}

}